import { post, get } from './url'
import Cookies from 'js-cookie'
import store from '@/store'
class Util
{
    // 解析分享id
    parseShareId()
    {
        let share_id = window.louie.share_id
        return post("share/find",{
            share_id
        })
            .then((res) => {
                let {code, data} = res.data
                if(code === 0) {
                    store.commit("share_uid",data.uid)
                    store.commit("enter_red",data.red_id)
                }
            })
    }

    /**
     * 分享
     * @param red_id
     */
    share(red_id)
    {
        if(process.env.NODE_ENV === 'production') {
            get("share/share", {
                red_id
            })
                .then((res) => {
                    let {code, data} = res.data
                    if(code === 0) {
                        window.wx.ready(()=>{
                            window.wx.updateAppMessageShareData(data)
                            window.wx.updateTimelineShareData(data)
                            window.wx.onMenuShareTimeline(data)
                            window.wx.onMenuShareAppMessage(data)
                        });
                    }
                })
        } else {
            console.log('加载分享')
        }
    }

    /**
     * 定位，获取用户所在省、市、区
     */
    location()
    {
        return new Promise((success)=>{
            if(store.state.area.length <= 0){
                let cacheArea = Cookies.get("kuyuan_hb_area")
                if(!cacheArea){
                    this.getLnglat()
                        .then((lnglat)=>{
                            let geocoder = new window.AMap.Geocoder();
                            geocoder.getAddress(lnglat,(status,result)=>{
                                if(status === 'complete'){
                                    let address = result.regeocode.addressComponent;
                                    let area = [address.province,address.city,address.district];
                                    Cookies.set("area",area,{ expires: 1 });
                                    store.commit("area",area);
                                    success(area);
                                }
                            });
                        })
                        .catch(() => {
                            let area;
                            if(process.env.NODE_ENV === 'development')
                                area = ['山西省','临汾市','尧都区'];
                            Cookies.set("area",area,{ expires: 1 });
                            store.commit("area",area);
                            success(area);
                        });
                }else{
                    store.commit("area",cacheArea);
                    success(cacheArea);
                }
            }else{
                success(store.state.area);
            }
        });
    }

    /**
     * 获取用户经纬度
     */
    getLnglat()
    {
        return new Promise((success,error)=>{
            if(process.env.NODE_ENV === 'development'){
                let Location = new window.AMap.Geolocation();
                Location.getCurrentPosition((status,result)=>{
                    if(status === 'complete'){
                        success([result.position.lng,result.position.lat]);
                    }else{
                        error(result);
                    }
                });
            }else{
                window.wx.ready(()=>{
                    window.wx.getLocation({
                        success:(res)=>{
                            success([res.longitude,res.latitude]);
                        }
                    });
                });
            }
        })
    }

    /**
     * 获取手机信息
     */
    getSystemInfo()
    {
        let data = {}
        let ua = navigator.userAgent
        let ios = ua.match(/\(i.+ (\d+_\d+(_\d)*) like Mac OS X\)/i)
        if (ios) {
            data.client = 'ios'
            data.version = ios[1].replace(/_/g,".")
        }
        let android = ua.match(/Android (\d+\.\d+(\.1)*)/i)
        if (android) {
            data.client = 'android'
            data.version = android[1]
        }
        if (window.wx) {
            let wxInfo = ua.match(/MicroMessenger\/(\d+\.\d+(\.5)*)/i)
            if(wxInfo) {
                data.wx_version = wxInfo[1]
            }
        } else {
            data.wx_version = false
        }
        return data
    }

    /**
     * 对比两个版本号 1=版本1大于版本2 0=两个版本号相等 -1 版本1小于版本2
     * @param version1
     * @param version2
     */
    versionCompare(version1,version2) {
        try {
            if(version1 && version2) {
                let float_v1 = parseFloat(version1)
                let float_v2 = parseFloat(version2)
                if (float_v1 > float_v2) {
                    return 1
                } else if(float_v1 < float_v2) {
                    return -1
                } else {
                    let v1 = version1.split('.')
                    let v2 =version2.split('.')
                    if(v1.length === 2 && v2.length === 2) {
                        return 0
                    }
                    let v1_last = 0
                    let v2_last = 0
                    if(v1[2]) v1_last = parseInt(v1[2])
                    if(v2[2]) v2_last = parseInt(v2[2])
                    if(v1_last > v2_last) {
                        return 1
                    } else if (v1_last < v2_last){
                        return -1
                    } else {
                        return 0
                    }
                }
            } else {
                throw new Error("请设置版本号")
            }
        }catch (e) {
            console.log(e)
        }
    }
}
export default new Util
